﻿@inject IBaiduOcr OcrService
@inject ToastService ToastService
@implements IDisposable

<ButtonUpload TValue="string" OnChange="@OnClickToUploadBlock" BrowserButtonText="VATInvoice" ShowUploadFileList="false" IsDisabled="IsLoading" BrowserButtonIcon="@Icon"></ButtonUpload>

@if (Invoice != null)
{
    <div class="row form-inline g-3 mt-0">
        <div class="col-12 col-sm-6">
            <Display ShowLabel="true" DisplayText="发票代码" @bind-Value="Invoice.InvoiceCode"></Display>
        </div>
        <div class="col-12 col-sm-6">
            <Display ShowLabel="true" DisplayText="发票号码" Value="Invoice.InvoiceNum"></Display>
        </div>
        <div class="col-12 col-sm-6">
            <Display ShowLabel="true" DisplayText="发票类型" @bind-Value="Invoice.ServiceType"></Display>
        </div>
        <div class="col-12 col-sm-6">
            <Display ShowLabel="true" DisplayText="联次信息" Value="Invoice.SheetNum"></Display>
        </div>
        <div class="col-12 col-sm-6">
            <Display ShowLabel="true" DisplayText="发票种类" @bind-Value="Invoice.InvoiceType"></Display>
        </div>
        <div class="col-12 col-sm-6">
            <Display ShowLabel="true" DisplayText="发票名称" Value="Invoice.InvoiceTypeOrg"></Display>
        </div>
        <div class="col-12 col-sm-6">
            <Display ShowLabel="true" DisplayText="发票标志" @bind-Value="Invoice.InvoiceTag"></Display>
        </div>
        <div class="col-12 col-sm-6">
            <Display ShowLabel="true" DisplayText="开票日期" Value="Invoice.InvoiceDate"></Display>
        </div>
        <div class="col-12">
            <Display ShowLabel="true" DisplayText="校验码" Value="Invoice.CheckCode"></Display>
        </div>
        <div class="col-12 col-sm-6">
            <Display ShowLabel="true" DisplayText="购方名称" @bind-Value="Invoice.PurchaserName"></Display>
        </div>
        <div class="col-12 col-sm-6">
            <Display ShowLabel="true" DisplayText="纳税人识别号" Value="Invoice.PurchaserRegisterNum"></Display>
        </div>
        <div class="col-12 col-sm-6">
            <Display ShowLabel="true" DisplayText="开户行及账号" @bind-Value="Invoice.PurchaserBank"></Display>
        </div>
        <div class="col-12 col-sm-6">
            <Display ShowLabel="true" DisplayText="购方地址" Value="Invoice.PurchaserAddress"></Display>
        </div>
        <div class="col-12 col-sm-6">
            <Display ShowLabel="true" DisplayText="销售方名称" @bind-Value="Invoice.SellerName"></Display>
        </div>
        <div class="col-12 col-sm-6">
            <Display ShowLabel="true" DisplayText="纳税人识别号" Value="Invoice.SellerRegisterNum"></Display>
        </div>
        <div class="col-12 col-sm-6">
            <Display ShowLabel="true" DisplayText="销售方及账号" @bind-Value="Invoice.SellerBank"></Display>
        </div>
        <div class="col-12 col-sm-6">
            <Display ShowLabel="true" DisplayText="销售方地址" Value="Invoice.SellerAddress"></Display>
        </div>
        <div class="col-12">
            <Display ShowLabel="true" DisplayText="备注" @bind-Value="Invoice.Remarks"></Display>
        </div>
        <div class="col-12 col-sm-6">
            <Display ShowLabel="true" DisplayText="价税合计" Value="Invoice.AmountInFiguers"></Display>
        </div>
        <div class="col-12 col-sm-6">
            <Display ShowLabel="true" DisplayText="合计金额" Value="Invoice.TotalAmount"></Display>
        </div>
        <div class="col-12 col-sm-6">
            <Display ShowLabel="true" DisplayText="合计税额" @bind-Value="Invoice.TotalTax"></Display>
        </div>
        <div class="col-12 col-sm-6">
        </div>
        @if (Invoice.CommodityName?.Any() ?? false)
        {
            <div class="col-12 col-sm-6">
                <Display ShowLabel="true" DisplayText="项目名称" Value="Invoice.CommodityName.First().CommodityName"></Display>
            </div>
        }
        @if (Invoice.CommodityTaxRate?.Any() ?? false)
        {
            <div class="col-12 col-sm-6">
                <Display ShowLabel="true" DisplayText="税率" Value="Invoice.CommodityTaxRate.First().CommodityTaxRate"></Display>
            </div>
        }
    </div>
}

@code {
    private InvoiceEntity? Invoice { get; set; }

    private async Task OnClickToUpload(UploadFile file)
    {
        if (file.File != null)
        {
            var payload = await file.GetBytesAsync(file.File.Size);
            if (payload?.Any() ?? false)
            {
                var result = await OcrService.CheckVatInvoiceAsync(payload);
                Invoice = result.Entity;
                if (result.Entity != null)
                {
                    await ToastService.Success("Vat Invoice", "VAT Invoice success!");
                    StateHasChanged();
                }
                else
                {
                    await ToastService.Information("Vat Invoice", $"{result.ErrorCode}: {result.ErrorMessage}");
                }
            }
        }
    }

    /*以下示例为本站特殊处理逻辑可不参考*/
    private bool IsLoading { get; set; }

    private string ButtonIcon { get; } = "fa-solid fa-cloud-arrow-up";

    private string LoadingIcon { get; } = "fa-solid fa-spinner fa-spin-pulse";

    private string Icon => IsLoading ? LoadingIcon : ButtonIcon;

    /// <summary>
    /// 取消请求令牌
    /// </summary>
    private CancellationTokenSource? TokenSource { get; set; }

    private async Task OnClickToUploadBlock(UploadFile file)
    {
        if (file.File != null)
        {
            // 设置 按钮禁用
            IsLoading = true;
            StateHasChanged();

            // 获得上传文件
            var payload = await file.GetBytesAsync(file.File.Size);
            if (payload?.Any() ?? false)
            {
                try
                {
                    TokenSource ??= new();
                    var result = await OcrService.CheckVatInvoiceAsync(payload, TokenSource.Token);
                    Invoice = result.Entity;
                    if (result.Entity != null)
                    {
                        await ToastService.Success("Vat Invoice", "VAT Invoice success!");
                    }
                    else
                    {
                        await ToastService.Information("Vat Invoice", $"{result.ErrorCode}: {result.ErrorMessage}");
                    }
                }
                catch (TaskCanceledException)
                {

                }
            }

            IsLoading = false;
            StateHasChanged();
        }
    }

    /// <summary>
    /// 关闭网页时调用
    /// </summary>
    /// <param name="disposing"></param>
    /// <returns></returns>
    protected virtual void Dispose(bool disposing)
    {
        if (disposing && TokenSource != null)
        {
            TokenSource.Cancel();
            TokenSource.Dispose();
        }
    }

    /// <summary>
    /// 关闭网页时调用
    /// </summary>
    /// <returns></returns>
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}
